package com.atguigu.mqdemo.consumer.config;

import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitConfig {

    /*
      给业务队列绑定死信队列
        创建死信队列:
            死信队列中的消息 代表消费失败的消息，可以通过定时任务定期处理
                也可以人工介入
     */
    //业务交换机+业务队列+绑定
    @Bean
    public Exchange demo2Exchange(){
        return ExchangeBuilder.topicExchange("demo2.exchange")
                .durable(true)
                .ignoreDeclarationExceptions()
                .build();
    }
    @Bean
    public Queue demo2Queue(){
        return QueueBuilder.durable("demo2.queue")
                //绑定死信交换机+死信路由key
                .deadLetterExchange("dead.exchange")
                .deadLetterRoutingKey("dead.msg")
                .build();
    }
    @Bean
    public Binding demo2Binding(Exchange demo2Exchange , Queue demo2Queue ){
        return BindingBuilder.bind(demo2Queue)
                .to(demo2Exchange)
                // * 通配一个一级的任意多个字符  # 通配任意多级多个字符
                .with("a.#")
                .noargs();

    }
    //死信交换机+队列+绑定
    @Bean
    public Exchange deadExchange(){
        return ExchangeBuilder.topicExchange("dead.exchange")
                    .ignoreDeclarationExceptions()
                    .durable(true)
                    .build();
    }
    @Bean
    public Queue deadQueue(){
        return QueueBuilder.durable("dead.queue")
                    .ttl(180*1000) //毫秒
                    .deadLetterExchange("delay.exchange")
                    .deadLetterRoutingKey("delay.msg")
                    .build();
    }
    @Bean
    public Binding deadBinding(Exchange deadExchange ,Queue deadQueue ){
        return BindingBuilder.bind(deadQueue).to(deadExchange).with("dead.msg").noargs();
    }
    //接收死信队列延迟消息的 延迟队列：
    @Bean
    public Exchange delayExchange(){
        return ExchangeBuilder.topicExchange("delay.exchange")
                .ignoreDeclarationExceptions()
                .durable(true)
                .build();
    }
    @Bean
    public Queue delayQueue(){
        return QueueBuilder.durable("delay.queue")
                .build();
    }
    @Bean
    public Binding delayBinding(Exchange delayExchange ,Queue delayQueue ){
        return BindingBuilder.bind(delayQueue).to(delayExchange).with("delay.msg").noargs();
    }
}
